﻿using System;
using System.Web;
using System.Configuration;
using System.Web.Caching;

namespace LanguageInfo.Common.Utilities
{
	/// <summary>
	/// Caching Utility
	/// </summary>
	public class CachingUtility
	{
		#region Cache
		public static Cache Cache
		{
			get
			{
				if (null != HttpContext.Current)
					return HttpContext.Current.Cache;
				else
					return HttpRuntime.Cache;
			}
		}
		public static string AppPrefix
		{
			get
			{
				return AppDomain.CurrentDomain.Id.ToString();
			}
		}
		#endregion

		#region  Get from cache
		/// <summary>
		/// Get a Cached Object
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static object Get(string key)
		{
			return Cache.Get(AppPrefix + key);
		}
		#endregion

		#region Set to cache

		/// <summary>
		/// Set Cache
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		public static void Set(string key, object value)
		{
			Cache.Insert(AppPrefix + key, value);
		}

		/// <summary>
		/// Set Object to Cache
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="cacheDependency"></param>
		public static void Set(string key, object value, CacheDependency cacheDependency)
		{
			Cache.Insert(AppPrefix + key, value, cacheDependency);
		}


		/// <summary>
		/// Set Object to Cache
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="cacheDependency"></param>
		/// <param name="dt"> absolute expiration datetime </param>
		public static void Set(string key, object value, CacheDependency cacheDependency, DateTime dt)
		{
			Cache.Insert(AppPrefix + key, value, cacheDependency, dt, TimeSpan.Zero);
		}
		
		/// <summary>
		/// Set Object to Cache
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="cacheDependency"></param>
		/// <param name="ts">sliding expiration timespan </param>
		public static void Set(string key, object value, CacheDependency cacheDependency, TimeSpan ts)
		{
			Cache.Insert(AppPrefix + key, value, cacheDependency, Cache.NoAbsoluteExpiration, ts);
		}
		
		/// <summary>
		/// Set Object to Cache
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="cacheDependency"></param>
		/// <param name="dt"> absolute expiration datetime </param>
		/// <param name="ts"> sliding expiration timespan </param>
		public static void Set(string key, object value, CacheDependency cacheDependency, DateTime dt, TimeSpan ts)
		{			
			Cache.Insert(AppPrefix + key, value, cacheDependency, dt, ts);
		}
		#endregion

		#region Remove
		/// <summary>
		/// Remove Cached Object by Key
		/// </summary>
		/// <param name="name"></param>
		public static void Remove(string key)
		{
			Cache.Remove(AppPrefix + key);
		}
		#endregion

		#region Contain
		/// <summary>
		/// Ensure the Cache Contain Object by Key
		/// </summary>
		/// <param name="key"></param>
		public static bool Contain(string key)
		{
			return Cache.Get(AppPrefix + key) != null;
		}
		#endregion
	}
}
